﻿
@using jianvgroup.com.Extensions
    @model jianvgroup.com.Models.BasePageModel

    @{if (Model != null && Model.TotalPages != 0)
        {
            <ul class="pagination">
                @{

                    @Url.CreatePageListTag(Model, Model.CurrentIndex - 1, false, Model.IsHasPrePage, "«")

                    if (Model.TotalPages <= Model.DisplayMaxPages)
                    {
                        for (int i = 1; i < Model.TotalPages; i++)
                        {

                            @Url.CreatePageListTag(Model, i, i == Model.CurrentIndex);
                    }
                }
                else
                {
                    if (Model.CurrentIndex - 1 < 5)
                    {
                        for (int i = 1; i <= Model.DisplayMaxPages - 1; i++)
                        {
                                @Url.CreatePageListTag(Model, i, i == Model.CurrentIndex);
                        }

                            @Url.CreatePageListTag(Model, Model.CurrentIndex, false, false, "...");
                    }
                    else
                    {
                            @Url.CreatePageListTag(Model, 1);


                        if (Model.CurrentIndex + (Model.DisplayMaxPages - 2) / 2 >= Model.TotalPages)
                        {
                            int page = Model.CurrentIndex - (Model.DisplayMaxPages - Model.TotalPages + Model.CurrentIndex - 1);

                            if (page > 1)
                            {
                                    @Url.CreatePageListTag(Model, Model.CurrentIndex, false, false, "...");
                            }

                            for (int i = page + 1; i < Model.TotalPages; i++)
                            {
                                    @Url.CreatePageListTag(Model, i, i == Model.CurrentIndex);
                            }
                        }
                        else
                        {
                            int page = Model.CurrentIndex - (Model.DisplayMaxPages - 2) / 2;
                            if (page > 2)
                            {
                                    @Url.CreatePageListTag(Model, Model.CurrentIndex, false, false, "...");
                            }

                            for (int i = page; i < Model.CurrentIndex + (Model.DisplayMaxPages - 2) / 2; i++)
                            {
                                    @Url.CreatePageListTag(Model, i, i == Model.CurrentIndex);
                            }
                                @Url.CreatePageListTag(Model, Model.CurrentIndex, false, false, "...");
                        }

                    }
                }

                    @Url.CreatePageListTag(Model, Model.TotalPages, Model.TotalPages == Model.CurrentIndex)
                    @Url.CreatePageListTag(Model, Model.CurrentIndex + 1, false, Model.IsHasNextPage, "»")

                }
            </ul>
                    }}